package com.cehome.cloudbox.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cehome.cloudbox.base.R;
import com.cehome.cloudbox.doman.dto.ProcAssignDto;
import com.cehome.cloudbox.doman.dto.ProcStateDto;
import com.cehome.cloudbox.doman.dto.ProcTaskHandleDto;
import com.cehome.cloudbox.doman.dto.ProcTaskPageShowVo;
import com.cehome.cloudbox.doman.vo.ProcInstInfo;
import com.cehome.cloudbox.doman.vo.ProcTaskPageQueryVo;
import com.cehome.cloudbox.flowalbe.FlowableReturnModel;
import com.cehome.cloudbox.service.IProcTaskService;
import com.cehome.cloudbox.service.ProcessIntanceLastService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 流程任务接口 控制器
 * </p>
 * @author zfl
 */
@Api(value = "流程任务操作API", tags = "流程任务接口")
@RestController
@RequestMapping("/flowable/task")
public class ProcTaskController {

    @Resource
    private IProcTaskService iProcTaskService;

    @Autowired
    ProcessIntanceLastService processIntanceLastService;

//      @ApiOperation(value = "审批--通过")
//      @PostMapping(value = "approveProcess")
//      public boolean approveProcess (@RequestBody ProcInstInfo instInfo) throws JsonProcessingException {
////          ProcInstInfo procInstInfo = new ProcInstInfo();
////          procInstInfo.setTaskId("39ee7aba-8b1f-11ec-8bc3-00ff49f432a0");
////          procInstInfo.setProdefKey("last0211");
////          //下一节点的审批人，多实例最后一个为准
////          List<String> huiqian = new ArrayList<>();
////          huiqian.add("11");
////          procInstInfo.setHuiqian(huiqian);
////          //抄送只能增加不能减少
////          List<String> chaosong = new ArrayList<>();
////          chaosong.add("7");
////          procInstInfo.setChaosong(chaosong);
////          //增加审批详情
////          Map<String,Object> info = new HashMap<>();
////          info.put("title","刘司机发起的申请请假2");
////          info.put("reason","生病请假");
////          Map<String,String> approve = new HashMap<>();
////          approve.put("approver","王总");
////          approve.put("result","同意");
////          approve.put("remark","多注意身体");
////          List<Map<String,String>> approves = new ArrayList<>();
////          approves.add(approve);
////          info.put("approve",approves);
////          String json = new ObjectMapper().writeValueAsString(info);
////          procInstInfo.setApplyInfo(json);
////          Map<String, Object> variables = new HashMap<>();
////          variables.put("weiwen","十斤大米");
////          procInstInfo.setVariables(variables);
////          procInstInfo.setUserId("123");
//          processIntanceLastService.approveProcess(instInfo);
//          return true;
//      }
//
//
//    @ApiOperation(value = "查询待办任务列表")
//    @GetMapping(value = "/todoList")
//    public List<Map<String,Object>> todoList(String userId) {
//        List<Map<String,Object>> instanceTo = processIntanceLastService.getInstanceTo(userId);
//        return instanceTo;
//    }
//
//
//    @ApiOperation(value = "查询流程中的变量填充表单")
//    @GetMapping(value = "/getInstanc")
//    public Map<String,Object> getInstanc(String procId) {
//        Map<String, Object> instanceParamById = processIntanceLastService.getInstanceParamById(procId);
//        return instanceParamById;
//    }
//
//    @ApiOperation(value = "查询流程中的变量填充表单")
//    @GetMapping(value = "/getFlowableReturnModel")
//    public FlowableReturnModel getFlowableReturnModel(String taskId){
//        FlowableReturnModel byTaskId = processIntanceLastService.getByTaskId(taskId);
//        return byTaskId;
//        //System.out.println(new ObjectMapper().writeValueAsString(byTaskId));
//        //{"assignListName":"huiqian1","huiq":false,"shenpiStr":"1,2,3","zhihui":"5,6,7"}
//    }



    @ApiOperation(value = "同意")
    @PostMapping(value = "/agree")
    public void agree(@RequestBody ProcInstInfo procInstInfo) {
        processIntanceLastService.approveProcess(procInstInfo);
    }

    @ApiOperation(value = "同意2")
    @PostMapping(value = "/agree2")
    public R<ProcStateDto> agree(@RequestBody ProcTaskHandleDto param) {
        return R.data(iProcTaskService.agree(param));
    }

//
//    @ApiOperation(value = "查询已办任务列表")
//    @GetMapping(value = "/doneList")
//    public Page<ProcTaskPageShowVo> doneList(ProcTaskPageQueryVo queryVo) {
//        return iProcTaskService.doneList(queryVo);
//    }
//

//    @ApiOperation(value = "同意")
//    @PostMapping(value = "/agree")
//    public R<ProcStateDto> agree(@RequestBody ProcTaskHandleDto param) {
//        return R.data(iProcTaskService.agree(param));
//    }
//
//    @ApiOperation(value = "拒绝")
//    @PostMapping(value = "/refuse")
//    public R<ProcStateDto> refuse(@RequestBody ProcTaskHandleDto param) {
//        return R.data(iProcTaskService.refuse(param));
//    }

//    @ApiOperation(value = "转办")
//    @PostMapping(value = "/assign")
//    public R<ProcStateDto> assign(@RequestBody ProcAssignDto param) {
//        return R.data(iProcTaskService.assign(param));
//    }

    @ApiOperation(value = "查询待办任务列表")
    @GetMapping(value = "/todoList")
    public R<Page<ProcTaskPageShowVo>> todoList(ProcTaskPageQueryVo queryVo) {
        return R.data(iProcTaskService.todoList(queryVo, true));
    }

    @ApiOperation(value = "查询已办任务列表")
    @GetMapping(value = "/doneList")
    public R<Page<ProcTaskPageShowVo>> doneList(ProcTaskPageQueryVo queryVo) {
        return R.data(iProcTaskService.doneList(queryVo));
    }

}
